﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Text.RegularExpressions;
using System.IO;

namespace WebCrawler
{
   public class HtmlDocRegEx : IHTMLDoc
    {
        // regex for relative url
        public const string URL_REGEX = "(href=\"/?(\\w+\\.?\\w+)\")";

        public HtmlDocRegEx(string url)
        {
            this.URL = url;
        }

        public void downloadPage()
        {
            IHttp conn = new HttpWeb();
            conn.Open(URL, 5000);

            StreamReader reader = new StreamReader(conn.GetStream());

            try
            {
                Document = reader.ReadToEnd();
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }

            Visited = true;
        }
        public string Document { get; set; }

        public string URL { set; get; }

        public bool Visited { get; set; }

        
        public List<string> GetAllLinks()
        {
            List<string> urlList = new List<string>();
            Regex regex = new Regex(URL_REGEX, RegexOptions.IgnoreCase);
            MatchCollection matches = regex.Matches(Document);

            for (int a = 0; a < matches.Count; a++)
            {
                string url = "";
                if (matches[a].Groups.Count >= 2)
                {
                    url = matches[a].Groups[2].ToString();
                    urlList.Add(getBaseUrl()+url);
                }
            }

            return urlList;
        }


        public bool Contains(string searchString)
        {
            int start = Document.IndexOf("<body>");
            int end = Document.IndexOf("</body>");
            int target = Document.IndexOf(searchString);

            if((start < target)&& (end > target)){
                return true;
            }

            return false;
        }

        private string getBaseUrl()
        {
            /* Find last occurence of '/' and cut the end */
            int pos = URL.LastIndexOf("/");
            return URL.Substring(0,pos+1);
        }
    }
}
